﻿using System.Collections.Generic;
using System.Linq;

namespace Core.Config.Model
{
    /// <summary>
    /// 数据库配置
    /// </summary>
    public class DaoConfig
    {

        #region 序列化属性
        /// <summary>
        /// 商品数据库配置信息
        /// </summary>
        public string MyDb { get; set; }

        /// <summary>
        /// 日志数据库配置信息
        /// </summary>
        public string MyDbLog { get; set; }

        /// <summary>
        /// 跨库链接
        /// </summary>
        public string CrossUrl { set; get; }

        #endregion

        #region redis配置

        /// <summary>
        /// 读redis库链接
        /// </summary>
        public string ReadServerConStr { set; get; }

        /// <summary>
        /// redis实例，redis://:r-bp110f905dd32364:
        /// </summary>
        public string RedisExample { set; get; }

        /// <summary>
        /// redis密码
        /// </summary>
        public string RedisPassword { set; get; }

        /// <summary>
        /// 写服务器密码
        /// </summary>
        public string WriteServerConStr { set; get; }

        /// <summary>
        /// 读应用池最大值
        /// </summary>
        public string MaxReadPoolSize { set; get; }

        /// <summary>
        /// 写应用池最大值
        /// </summary>
        public string MaxWritePoolSize { set; get; }

        /// <summary>
        /// 是否自动启动
        /// </summary>
        public string AutoStart { set; get; }

        #endregion

        /// <summary>
        /// js版本号
        /// </summary>
        public string JsVersion { set; get; }

        #region 数据库名称

        /// <summary>
        /// 商品数据库
        /// </summary>
        public string MyDbCatalog => GetCatagoryValue(MyDb.Split(';').ToList());

        /// <summary>
        /// 日志数据库
        /// </summary>
        public string MyDbLogCatalog => GetCatagoryValue(MyDbLog.Split(';').ToList());

        #endregion
        /// <summary>
        /// 获取数据库名称
        /// </summary>
        /// <param name="config">config内容</param>
        private string GetCatagoryValue(List<string> config)
        {
            var dbName = "";
            var dbValue = config.Where(n => n.Contains("Initial Catalog"));
            if (dbValue.Any())
            {
                var catagoryNames = dbValue.FirstOrDefault().Split('=');
                if (catagoryNames.Length >= 2)
                {
                    dbName = catagoryNames[1];
                }
            }
            var url = $@"{CrossUrl}.{dbName}.dbo.";
            return url;
        }
    }
}
